<template>
  <div class="content" ref="content">
    <div class="table-default" v-if="tableData">
      <el-button type="primary" class="backBtn" @click="backHandler">返回</el-button>
      <el-button type="primary" class="backBtn" @click="printHandler">箱号补打</el-button>
      <!-- <div class="search-hidden" ref="searchbox"> -->
      <div id="base" ref="base">
        <el-descriptions :column="3" border title="基本信息">
          <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="单号">{{ tableData.docno }}</el-descriptions-item>
          <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="工单号">{{ tableData.production_no }}</el-descriptions-item>
          <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="创建时间">{{ tableData.created_at }}</el-descriptions-item>
          <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="状态">{{ statusFormatter[tableData.kingdee_status] }}</el-descriptions-item>
        </el-descriptions>
      </div>
      <!-- </div> -->

      <div id="details" ref="details">
        <el-tabs v-model="activeName" class="demo-tabs">
          <el-tab-pane label="生产汇报明细" name="details">
            <el-table :data="detailData" border>
              <el-table-column prop="material_no" label="品号" min-width="130" />
              <el-table-column prop="material_name" label="品名" min-width="400" />
              <el-table-column prop="material.funit_name" label="单位" min-width="90" />
              <el-table-column prop="material.fspecification" label="规格" min-width="150" />
              <el-table-column prop="qty" label="汇报数量" width="100">
                <template #default="scope">
                  {{ Number(detailData[scope.$index].qty) }}
                </template>
              </el-table-column>
              <el-table-column prop="inbound_qty" label="入库数量" width="100">
                <template #default="scope">
                  {{ Number(detailData[scope.$index].inbound_qty) }}
                </template>
              </el-table-column>
              <el-table-column prop="cases" label="箱数" width="80" />
              <el-table-column prop="people" label="人数" width="80" />
              <el-table-column prop="work_hours" label="工时" width="80">
                <template #default="scope">
                  {{ Number(detailData[scope.$index].work_hours) }}
                </template>
              </el-table-column>
              <el-table-column prop="total_hours" label="总工时" width="80" />
              <el-table-column prop="creator" label="创建人" min-width="130" />
              <el-table-column prop="created_at" label="创建时间" min-width="170" />
              <el-table-column prop="updated_at" label="更新时间" min-width="170" />
            </el-table>
            <Paginate />
          </el-tab-pane>
          <el-tab-pane label="箱号列表" name="barcode">
            <el-table :data="BoxData" border @selection-change="handleSelectionChange" v-loading="selfLoading">
              <el-table-column type="selection" width="55" />
              <!--              <el-table-column prop="id" label="id" min-width="120" />-->
              <!--              <el-table-column prop="report_order_id" label="汇报单号id" min-width="120" />-->
              <el-table-column prop="lot_num" label="箱号" width="200" />
              <el-table-column prop="material_no" label="品号" min-width="100" />
              <el-table-column prop="material.fname" label="品名" min-width="100" />
              <el-table-column prop="material.funit_name" label="单位" min-width="100" />
              <el-table-column prop="material.fspecification" label="规格" min-width="100" />
              <el-table-column prop="qty" label="数量" width="100">
                <template #default="scope">
                  {{ Number(scope.row.qty) }}
                </template>
              </el-table-column>
              <el-table-column prop="creator" label="创建人" min-width="130" />
              <el-table-column prop="created_at" label="创建时间" min-width="170" />
              <el-table-column prop="updated_at" label="更新时间" min-width="170" />
            </el-table>
            <!-- <Paginate /> -->
            <el-pagination
              v-model:current-page="pages.current"
              v-model:page-size="pages.limit"
              layout="total, prev, pager, next, jumper"
              :total="pages.total"
              @current-change="handleCurrentChange"
              background
            />
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>

    <el-dialog v-model="printShow" title="预览" width="60%">
      <barCode :barcodeList="barcodeList" :PrintNow="PrintNow" :output="output" @send-data="getDataHandler"></barCode>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="warning" @click="PrintNow = true">确认打印</el-button>
          <!-- <el-button type="warning" @click="output = true" >确认导出</el-button> -->
          <el-button @click="printShow = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { computed, onMounted, ref, reactive } from 'vue'
import { useGetList } from '/admin/composables/curd/useGetList'
import { useDestroy } from '/admin/composables/curd/useDestroy'
import { useOpen } from '/admin/composables/curd/useOpen'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import http from '/admin/support/http'
import barCode from './barCode.vue'

const router = useRouter()
const route = useRoute()
const selfLoading = ref(false)

const api = `production/report/order/${route.query.id}`
const { data, query, search, reset, loading } = useGetList(api)
const { destroy, deleted } = useDestroy()
const { open, close, title, visible, id } = useOpen()

const tableData = computed(() => data.value?.data)

const barArr = ref([])
const printShow = ref(false)
const PrintNow = ref(false)
const output = ref(false)

console.log('获取到的tableData：', tableData)
// const rowInfo = JSON.parse(route.query.rowInfo)
// console.log(rowInfo)

const detailData = ref([])

const getDetail = () => {
  // /aip/production/pick/detail?order_id=领料单id
  const api2 = `production/report/detail?order_id=${route.query.id}?`
  http.get(api2).then(r => {
    console.log(r.data.data)
    detailData.value = r.data.data
  })
}

getDetail()

const BoxData = ref([])

let pages = reactive({
  totla: null,
  current: 1,
  limit: 50,
})

const getBoxData = () => {
  selfLoading.value = true
  let data = {
    limit: pages.limit,
    page: pages.current,
  }
  const api2 = `production/report/box?report_order_id=${route.query.id}`
  http.get(api2,data).then(r => {
    pages.total = r.data.total
    console.log('请求汇报箱号列表：', r.data.data)
    BoxData.value = r.data.data
  selfLoading.value = false
  })
}

getBoxData()

const handleCurrentChange = val => {
  pages.current = val
  getBoxData()
  // console.log(`current page: ${val}`)
}

let BoxList = ref([])

const handleSelectionChange = val => {
  console.log(val)

  BoxList.value = val.map(ele => ele.id)

  console.log('选中箱号id', BoxList.value)
}

const barcodeList = ref([])

// 获取补打箱号的数据
const getReportPrint = ids => {
  let data = {
    id: ids,
  }
  http.post(`production/report/print`, data).then(r => {
    console.log('请求汇报箱号列表：', r.data.data)
    barcodeList.value = r.data.data
    printShow.value = true
  })
}

const printHandler = () => {
  if (BoxList.value.length == 0) {
    ElMessage.warning('请先选择箱号')
  } else {
    getReportPrint(BoxList.value)
  }
}

const statusFormatter = {
  0: '待推送',
  1: '推送成功',
  2: '推送失败',
}

const searchbox = ref(null)

// console.log('路由参数接收', route.query.id)

const backHandler = () => {
  router.push('/production/ProductionReport')
}

const activeName = ref('details')
const base = ref(null)
const details = ref(null)
const content = ref()

const getDataHandler = () => {
  console.log('接收到子组件传值')
  PrintNow.value = false
}

onMounted(() => {
  search()
  deleted(reset)
})
</script>

<style lang="css">
/* .content{
  border: 2px solid red;
 overflow:hidden;
} */
.backBtn {
  margin: 20px 10px;
}

h2 {
  font-weight: bold;
}
#base {
  margin-bottom: 30px;
  /* border: 2px solid green; */
}
#details {
  /* border: 2px solid pink; */
}

.tabs {
  padding: 10px 0 0;
  /* border: 2px solid #000; */
  z-index: 999;
  background-color: #fff;
  position: sticky;
  top: -15px;
}

.tagRow {
  display: inline-block;
  /* border: 1px solid #000; */
  margin: 20px 10px;
}

.tagRow ul {
  display: flex;
  border: 1px solid #eee;
}
.tagRow ul li {
  margin: 0 5px;
  /* border: 1px solid #eee; */
  padding: 5px 10px;
  /* border-radius: 5px; */
}
.active {
  background-color: #409eff;
  color: #fff;
}
.labelClassNAme {
  width: 130px;
}
.contentClassName {
  width: 170px;
}
</style>
